<?php

require_once( '../../libs/smarty.inc' );
require_once( '../../libs/smt_variables.php' );
require_once( '../../libs/Class_DB.php' );
require_once( '../../libs/Class_ERROR.php' );
require_once( '../../libs/login.inc.php' );

if( !in_array( "M", $_SESSION['ADMIN_LOGIN']['AUTH_CD'] ) ) exit;

if( $_SERVER['REQUEST_METHOD'] == 'GET' ){
	
	$db = new Class_DB;
	$price_no = $_GET['price_no'];
	$consumer_id = $_GET['consumer_id'];
	
	$mod = $_GET['mod'];
	
	if($mod == 'quoteprint'){
		$db->query("UPDATE p_consumer_head set quoteprint = 1 where price_no ='$price_no'");
		$smarty->assign('phase', 'print');
	}
	
	$sql1 = "SELECT * FROM p_consumer_head WHERE price_no ='$price_no' AND consumer_id=$consumer_id ";
	$sql2 = "SELECT * FROM p_consumer WHERE consumer_id=$consumer_id";
	$sql3 = "SELECT * FROM p_consumer_detail as a WHERE a.price_no='$price_no'";
	$result1 = $db->query($sql1);
	$result2 = $db->query($sql2);
	$result3 = $db->query($sql3);
	
	$row1 = mysql_fetch_array($result1);
	$row1['m_date']=$row1['m_date']==NULL?"":date('Y/m/d',strtotime($row1['m_date']));
	$row2 = mysql_fetch_array($result2);
	$row3 = array();
	while( $row = mysql_fetch_array($result3,MYSQL_ASSOC) ){
		$row['money'] = $row['refer_price']*$row['discount']*$row['quantity'];
		$row['cmoney'] = number_format(intval($row['refer_price']*$row['discount']));
		$row['profit'] = round((($row['money']-$row['cost_price']*$row['quantity'])/$row['money'])*100,2);
		$row['money'] = number_format(intval($row['refer_price']*$row['discount']*$row['quantity']));
		$row['refer_price'] = number_format(intval($row['refer_price']));
		$row['cost_price'] = number_format(intval($row['cost_price']));
		
		$row3[] = $row;
			
	
	}
	
	$db->close();
	$price_no = $row1['price_no'];
	$staff_name = $row1['producer'];
	$smarty->assign('forms', $row1);
	$smarty->assign('consumer', $row2);
	$smarty->assign('price_no', $price_no);
	$smarty->assign('staff_name', $staff_name);
	$smarty->assign('detail', $row3);
	$smarty->assign('num', count($row3)+1);
	
}


$menu_html = file_get_contents( $_SERVER['DOCUMENT_ROOT'].'/templates/web-admin/menu.html' );
$smarty->assign('menu_html', $menu_html);
$smarty->display('web-admin/price_book.html');


?>